package com.android.justcook.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

	public static final String TABLE_RECIPES = "recipes";
	public static final String COLUMN_ID = "id";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_POINTS = "points";
	public static final String COLUMN_INGREDIENTS = "ingredients";
	public static final String COLUMN_METHOD = "method";
	public static final String COLUMN_DONE = "done";
	public static final String COLUMN_FAVOURITE = "favourite";
	public static final String COLUMN_PICTURE = "picture";
	public static final String COLUMN_EXPIRE = "expire";
	public static final String COLUMN_EXPIRE_TS = "expire_ts";
	public static final String COLUMN_CID = "category_id";

	public static final String TABLE_CATEGORIES = "categories";
	public static final String CATEGORIES_CID = "category_id";
	public static final String CATEGORIES_RID = "recipe_id";

	private static final String DATABASE_NAME = "recipes.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String DATABASE_CREATE = "create table "
			+ TABLE_RECIPES + "(" + COLUMN_ID + " text primary key, "
			+ COLUMN_ID + " integer not null, " + COLUMN_NAME
			+ " text not null, " + COLUMN_POINTS + " integer not null, "
			+ COLUMN_INGREDIENTS + " text not null, " + COLUMN_METHOD
			+ " text not null, " + COLUMN_DONE + " integer not null, "
			+ COLUMN_FAVOURITE + " integer not null, " + COLUMN_PICTURE
			+ " text not null, " + COLUMN_EXPIRE + " text not null, "
			+ COLUMN_EXPIRE_TS + " integer not null, " + ");";
	
	private static final String DABASE_CATEGORIES_CREATE = "create table "
			+ TABLE_CATEGORIES + "(" + CATEGORIES_CID + " integer primary key, "
			+ CATEGORIES_RID + " integer" + ");";

	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
		database.execSQL(DABASE_CATEGORIES_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(MySQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECIPES);
		onCreate(db);
	}

}